package cn.kgc.interceptor;

import cn.kgc.util.Common;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 自定义拦截器
 */
public class SysInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //请求拦截
        //获取Session对象
        HttpSession session=request.getSession();
        //判断用户是否登录
        Object object = session.getAttribute(Common.LOGIN_USER);
        if (null == object) {
            //用户没有登录，登录超时
            //跳转到提示页面
            response.sendRedirect(request.getContextPath()+"/401.jsp");
            return false;
        }
        return true;
    }
}
